const Chat = function (socket) {
    this.socket = socket
}

// 发送聊天信息
Chat.prototype.sendMessage = function (room, text) {
    let message = {
        room: room,
        text: text
    }
    this.socket.emit('message', message)
}

// 变更房间
Chat.prototype.changeRoom = function (room) {
    this.socket.emit('join', {
        newRoom: room
    })
}

// 处理聊天命令
Chat.prototype.processCommand = function (command) {
    let words = command.split(' ')
    let commandTemp = words[0].substring(1, words[0].length).toLowerCase() // 从第一个单词开始解析命令
    let message = false

    switch(commandTemp) {
        case 'join': // 处理房间的变换/创建
            words.shift();
            let room = words.join(' ');
            this.changeRoom(room);
            break;
        case 'nick': // 处理更名尝试
            words.shift();
            let name = words.join(' ');
            this.socket.emit('nameAttempt', name);
            break;
        default: // 如果命令无法识别，返回错误消息
            message = 'Unrecognized command.';
            break;
    }

    return message
}